// 游戏配置
const GAME_CONFIG = {
  GRID_SIZE: 20,
  INITIAL_SNAKE_LENGTH: 3,
  INITIAL_DIRECTION: 'right',
  GAME_SPEED: 150,
  FOOD_COLOR: '#e74c3c',
  SNAKE_COLOR: '#2ecc71',
  SNAKE_HEAD_COLOR: '#27ae60'
}

// 游戏状态类
export class SnakeGame {
  constructor(canvas) {
    this.canvas = canvas
    this.ctx = canvas.getContext('2d')
    this.gridSize = GAME_CONFIG.GRID_SIZE
    this.snake = []
    this.food = null
    this.direction = GAME_CONFIG.INITIAL_DIRECTION
    this.score = 0
    this.gameOver = false
    this.gameLoop = null
    
    this.init()
  }

  init() {
    // 初始化蛇的位置
    const startX = Math.floor(this.canvas.width / (2 * this.gridSize)) * this.gridSize
    const startY = Math.floor(this.canvas.height / (2 * this.gridSize)) * this.gridSize
    
    for (let i = 0; i < GAME_CONFIG.INITIAL_SNAKE_LENGTH; i++) {
      this.snake.push({
        x: startX - (i * this.gridSize),
        y: startY
      })
    }
    
    this.generateFood()
  }

  generateFood() {
    const maxX = Math.floor(this.canvas.width / this.gridSize)
    const maxY = Math.floor(this.canvas.height / this.gridSize)
    
    do {
      this.food = {
        x: Math.floor(Math.random() * maxX) * this.gridSize,
        y: Math.floor(Math.random() * maxY) * this.gridSize
      }
    } while (this.snake.some(segment => 
      segment.x === this.food.x && segment.y === this.food.y
    ))
  }

  update() {
    if (this.gameOver) return

    // 获取蛇头
    const head = { ...this.snake[0] }

    // 根据方向移动蛇头
    switch (this.direction) {
      case 'up':
        head.y -= this.gridSize
        break
      case 'down':
        head.y += this.gridSize
        break
      case 'left':
        head.x -= this.gridSize
        break
      case 'right':
        head.x += this.gridSize
        break
    }

    // 检查碰撞
    if (this.checkCollision(head)) {
      this.gameOver = true
      return
    }

    // 移动蛇
    this.snake.unshift(head)

    // 检查是否吃到食物
    if (head.x === this.food.x && head.y === this.food.y) {
      this.score += 10
      this.generateFood()
    } else {
      this.snake.pop()
    }
  }

  checkCollision(head) {
    // 检查是否撞墙
    if (
      head.x < 0 ||
      head.x >= this.canvas.width ||
      head.y < 0 ||
      head.y >= this.canvas.height
    ) {
      return true
    }

    // 检查是否撞到自己
    return this.snake.some(segment =>
      segment.x === head.x && segment.y === head.y
    )
  }

  draw() {
    // 清空画布
    this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height)

    // 绘制食物
    this.ctx.fillStyle = GAME_CONFIG.FOOD_COLOR
    this.ctx.fillRect(
      this.food.x,
      this.food.y,
      this.gridSize - 1,
      this.gridSize - 1
    )

    // 绘制蛇
    this.snake.forEach((segment, index) => {
      this.ctx.fillStyle = index === 0 
        ? GAME_CONFIG.SNAKE_HEAD_COLOR 
        : GAME_CONFIG.SNAKE_COLOR
      this.ctx.fillRect(
        segment.x,
        segment.y,
        this.gridSize - 1,
        this.gridSize - 1
      )
    })
  }

  start() {
    if (this.gameLoop) return
    
    this.gameLoop = setInterval(() => {
      this.update()
      this.draw()
    }, GAME_CONFIG.GAME_SPEED)
  }

  pause() {
    if (this.gameLoop) {
      clearInterval(this.gameLoop)
      this.gameLoop = null
    }
  }

  changeDirection(newDirection) {
    const opposites = {
      up: 'down',
      down: 'up',
      left: 'right',
      right: 'left'
    }

    if (newDirection !== opposites[this.direction]) {
      this.direction = newDirection
    }
  }

  reset() {
    this.snake = []
    this.score = 0
    this.direction = GAME_CONFIG.INITIAL_DIRECTION
    this.gameOver = false
    this.init()
  }
} 